package si.assign1;

import java.sql.Clob;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import si.assign1.manager.ProductSectionCreator;
import si.assign1.manager.ProductSectionVerifier;
import si.assign1.model.Category;
import si.assign1.model.Product;
import si.assign1.model.ProductType;
import si.assign1.model.ProductTypeAttribute;
import si.assign1.model.ProductTypeAttributeValue;


public class Main {
	
	/**
	 * @param args
	 */
	public static void createData() {
		
		/*
		 * data for product types
		 */
		String[] productTypeNames = {
				"Food", 
				"Clothing", 
				"Computer"
		};
		
		/*
		 * data for product type attributes
		 */
		String[] productTypeAttributeNames = {
				"Quality",
				"Color",
		};
		
		/*
		 * data for product type attribute values
		 */
		String[][] attributeValues = {
				{
					"Old",
					"New",
					"Good"
				},
				{
					"Red",
					"Yellow",
					"Blue"
				}
		};
		
		/*
		 * data for products
		 */
		String[] foodNames = {
				"Bread", 
				"Cab", 
				"Cake"
		};
		
		String[] clothingNames = {
				"Pants", 
				"Jacket", 
				"Suit"
		};
		
		String[] computerNames = {
				"Dell", 
				"Acer", 
				"IPad"
		};
		
		/*
		 * data for categories
		 */
		String[] categoryNames = {
				"Overseas", 
				"Domestic"
		};
		
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("assign1PU");
		
		EntityManager em = emf.createEntityManager();
		
		ProductSectionCreator psc = new ProductSectionCreator(em);
		
		/*
		 * clean up the tables to avoid duplication of rows
		 */
		psc.CleanUp();
		
		em.getTransaction().begin();

		float pp = 1, sp = 2;
		int i, j = 0;
		
		/*
		 * settings for food product type
		 */
		ProductType food = psc.createProductType(productTypeNames[0]);
		for (String attrName: productTypeAttributeNames) {
			ProductTypeAttribute pta = psc.createProductTypeAttribute(attrName, 'Y', food);
		}
		for (String foodName: foodNames) {
			Product p = psc.createProduct(foodName, "sku_" + foodName, "desc_" + foodName, pp, sp, food);
			i = 0;
			for (String attrName: productTypeAttributeNames) {
				psc.setProductTypeAttributeValueForProduct(p, attrName, 
						attributeValues[i][(j++)%attributeValues[i].length]);
				i++;
			}
			for (String categoryName: categoryNames) {
				psc.assignCategoryToProduct(categoryName, null, p);
			}
			pp++;
			sp++;
		}
		
		/*
		 * settings for clothing product type
		 */
		ProductType clothing = psc.createProductType(productTypeNames[1]);
		for (String attrName: productTypeAttributeNames) {
			ProductTypeAttribute pta = psc.createProductTypeAttribute(attrName, 'Y', clothing);
		}
		for (String clothingName: clothingNames) {
			Product p = psc.createProduct(clothingName, "sku_" + clothingName, "desc_" + clothingName, pp, sp, clothing);
			i = 0;
			for (String attrName: productTypeAttributeNames) {
				psc.setProductTypeAttributeValueForProduct(p, attrName, 
						attributeValues[i][(j++)%attributeValues[i].length]);
				i++;
			}
			for (String categoryName: categoryNames) {
				psc.assignCategoryToProduct(categoryName, null, p);
			}
			pp++;
			sp++;
		}
		
		/*
		 * settings for computer product type
		 */
		ProductType computer = psc.createProductType(productTypeNames[2]);
		for (String attrName: productTypeAttributeNames) {
			ProductTypeAttribute pta = psc.createProductTypeAttribute(attrName, 'Y', computer);
		}
		for (String computerName: computerNames) {
			Product p = psc.createProduct(computerName, "sku_" + computerName, "desc_" + computerName, 
					pp, sp, computer);
			i = 0;
			for (String attrName: productTypeAttributeNames) {
				psc.setProductTypeAttributeValueForProduct(p, attrName, 
						attributeValues[i][(j++)%attributeValues[i].length]);
				i++;
			}
			for (String categoryName: categoryNames) {
				psc.assignCategoryToProduct(categoryName, null, p);
			}
			pp++;
			sp++;
		}
			
		System.out.print("now committing...\n");
		em.getTransaction().commit();
		
		System.out.printf("commitment finished.\n");
		em.close();
		
		emf.close();
	}
	
	public static void reviewData() {
		System.out.print("{verify with JDBC}");
		
		ProductSectionVerifier.verifyWithJdbc();
		
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("assign1PU");
		
		EntityManager em = emf.createEntityManager();

		ProductSectionVerifier verifier = new ProductSectionVerifier(em);

		System.out.print("{list all objects}");
		verifier.ListAllObjects();
		
		System.out.print("{view by product types}");
		verifier.GetProductsWithAllInfo("Food");
		verifier.GetProductsWithAllInfo("Clothing");
		verifier.GetProductsWithAllInfo("Computer");
				
		em.close();
		
		emf.close();
		
	}
	
	
	public static void main(String[] args) {
		createData();
		reviewData();
	}
	
}